package com.example.websockettest.tree;

import com.alibaba.fastjson.JSON;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.*;
import java.util.stream.Collectors;

/* ━━━━━━佛祖保佑━━━━━━
 *                  ,;,,;
 *                ,;;'(    社
 *      __      ,;;' ' \   会
 *   /'  '\'~~'~' \ /'\.)  主
 * ,;(      )    /  |.     义
 *,;' \    /-.,,(   ) \    码
 *     ) /       ) / )|    农
 *     ||        ||  \)
 *     (_\       (_\
 * ━━━━━━永无BUG━━━━━━
 * @author ：zjq
 * @date ：2022/2/10 22:48
 * @description: TODO
 * @version: V1.0
 * @slogan: 天下风云出我辈，一入代码岁月催
 */
@Slf4j
public class Main {
    private static final Logger logger = LoggerFactory.getLogger(Main.class);

    public static void main(String[] args) {
        // 建造对象
        GoodsStockLabelTree g0 = new GoodsStockLabelTree();
        g0.setTspId("PLM_ccc");
        g0.setTitle("非常规标签");
        g0.setSourceSystem("PLM");
        g0.setId(100);

        //一级
        GoodsStockLabelTree g1 = new GoodsStockLabelTree();
        g1.setTspId("PLM_buyer");
        g1.setTitle("常规标签");
        g1.setSourceSystem("PLM");
        g1.setId(101);

        // 二级
        GoodsStockLabelTree g2 = new GoodsStockLabelTree();
        g2.setTspId("PLM_doll01");
        g2.setTspPid("PLM_doll01");
        g2.setTitle("常规标签-01");
        g2.setSourceSystem("PLM");
        g2.setId(102);

        // 二级   UNIQUE `uk_title_pid_tsp` (`title`, `pid`, `tsp_id`),
        GoodsStockLabelTree g3 = new GoodsStockLabelTree();
        g3.setTspId("PLM_doll02");
        g3.setTspPid("PLM_buyer");
        g3.setTitle("常规标签-02");
        g3.setSourceSystem("PLM");
        g3.setId(103);

        // 三级
        GoodsStockLabelTree g4 = new GoodsStockLabelTree();
        g4.setTspId("PLM_doll02-01"); //  自己的id 要是换成父级的id就会无限递归
//        g4.setTspId("PLM_buyer");  // PLM_buyer 或者  PLM_doll02 都会无限递归
        g4.setTspPid("PLM_doll02");
        g4.setTitle("常规标签-02-01");
        g4.setSourceSystem("PLM");
        g4.setId(104);

        // 三级 递归死循环例子 如果 父id 和id 相同,且是别的标签的id  则会出现
        GoodsStockLabelTree g44 = new GoodsStockLabelTree();
        g44.setTspId("PLM_doll02-01"); // PLM_doll02 这个是 g4 的id 会无线地柜
        g44.setTspPid("PLM_doll02-01");
        g44.setTitle("常规标签-02-02");
        g44.setSourceSystem("PLM");
        g44.setId(105);


        GoodsStockLabelTree g5 = new GoodsStockLabelTree();
        g5.setTspId("DMS_1");
        g5.setTspPid("");
        g5.setTitle("季节标签");
        g5.setSourceSystem("DMS");
        g5.setId(1);


        GoodsStockLabelTree g6 = new GoodsStockLabelTree();
        g6.setTspId("DMS_2");
        g6.setTspPid("");
        g6.setTitle("国家标签");
        g6.setSourceSystem("DMS");
        g6.setId(2);


        GoodsStockLabelTree g7 = new GoodsStockLabelTree();
        g7.setTspId("DMS_5");
        g7.setTspPid("DMS_1");
        g7.setTitle("春下");
        g7.setSourceSystem("DMS");
        g7.setId(5);


        List<GoodsStockLabelTree> typeList = new ArrayList<>();

        typeList.add(g0);
        typeList.add(g1);
        typeList.add(g2);
        typeList.add(g3);
        typeList.add(g4);
        typeList.add(g44);
        typeList.add(g5);
        typeList.add(g6);
        typeList.add(g7);


        /**
         * 备货管理页面：  传给后端的是 id，全部就是所有id
         * 订单标签规则：  保存规则，全部 -1 ； 其他的是中文名
         *               com.shein.dms.api.orderexternal.service.impl.OrderExternaServiceImpl#bindOrderLabel
         *               订单计算紧急任务，找的是tsp_label_id,传的也是tsp_label_id,前端要区分
         */

        // 1 过滤掉 id 和父id相同的脏数据 , 再  tspId   去重
        List<GoodsStockLabelTree> filterList = typeList.stream()
//                .filter(s -> !s.getTspId().equalsIgnoreCase(s.getTspPid()))
                .collect(Collectors.
                        collectingAndThen(
                                Collectors.toCollection(() ->
                                        new TreeSet<>(Comparator.comparing(GoodsStockLabelTree::getTspId))),
                                ArrayList::new)
                );


        Map<String, List<GoodsStockLabelTree>> treeMap =
                filterList.stream().collect(Collectors.groupingBy(GoodsStockLabelTree::getSourceSystem));

        Map<String, List<GoodsStockLabelTree>> newMap = new HashMap<>();

        treeMap.forEach((k, v) -> {
            //获取父节点
            List<GoodsStockLabelTree> collect = v.stream()
                    .filter(m -> StringUtils.isBlank(m.getTspPid()))
                    .peek(m -> m.setChildList(getChildrens(m, v)))
                    .collect(Collectors.toList());
            newMap.put(k, collect);
        });

        System.out.println(JSON.toJSONString(newMap));

    }

    /**
     * 递归查询节点
     *
     * @param root 节点
     * @param all  所有节点
     * @return 节点信息
     */
    private static List<GoodsStockLabelTree> getChildrens(GoodsStockLabelTree root, List<GoodsStockLabelTree> all) {
        // lambad简化版
//        List<GoodsStockLabelTree> collect = all.stream()
//                .filter(m -> StringUtils.equalsIgnoreCase(m.getTspPid(), root.getTspId()))
//                .peek(m -> m.setChildList(getChildrens(m, all)))
//                .collect(Collectors.toList());
        // for 版 找到 集合中 父id 等于 当前id 的
        List<GoodsStockLabelTree> collect = all.stream()
                .filter(m -> StringUtils.equalsIgnoreCase(root.getTspId(), m.getTspPid())).collect(Collectors.toList());
        for (GoodsStockLabelTree m : collect) {
            List<GoodsStockLabelTree> childrens = getChildrens(m, all);
            m.setChildList(childrens);
        }
        return collect; // 返回找到的 集合
    }


}
